package com.ruoyi.patrol.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.patrol.domain.PtCheckQuestion;
import com.ruoyi.patrol.domain.PtStaff;
import com.ruoyi.patrol.service.IPtCheckQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 专项检查问题Controller
 *
 * @author Du
 * @date 2023-11-18
 */
@RestController
@RequestMapping("/patrol/checkQuestion")
public class PtCheckQuestionController extends BaseController {
    @Autowired
    private IPtCheckQuestionService ptCheckQuestionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询专项检查问题列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:checkQuestion:list')")
    @GetMapping("/list")
    public TableDataInfo list(PtCheckQuestion ptCheckQuestion) {
        startPage();
        List<PtCheckQuestion> list = ptCheckQuestionService.selectPtCheckQuestionList(ptCheckQuestion);
        return getDataTable(list);
    }

    /**
     * 导入问题列表模板
     *
     * @return
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<PtCheckQuestion> util = new ExcelUtil<PtCheckQuestion>(PtCheckQuestion.class);
        return util.importTemplateExcel("检查问题上传模板");
    }

    /**
     * 导入专项检查问题列表
     */
    @Log(title = "专项检查问题", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importQuestion(MultipartFile file, boolean updateSupport, @RequestParam("cId") Long cId) throws Exception {
        ExcelUtil<PtCheckQuestion> util = new ExcelUtil<PtCheckQuestion>(PtCheckQuestion.class);
        List<PtCheckQuestion> questionList = util.importExcel(file.getInputStream());
        String username = tokenService.getLoginUser(ServletUtils.getRequest()).getUsername();
        String msg = ptCheckQuestionService.importQuestion(questionList, updateSupport, cId, username);
        return AjaxResult.success(msg);
    }

    /**
     * 导出专项检查问题列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:checkQuestion:export')")
    @Log(title = "专项检查问题", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PtCheckQuestion ptCheckQuestion) {
        List<PtCheckQuestion> list = ptCheckQuestionService.selectPtCheckQuestionList(ptCheckQuestion);
        ExcelUtil<PtCheckQuestion> util = new ExcelUtil<PtCheckQuestion>(PtCheckQuestion.class);
        return util.exportExcel(list, "checkQuestion");
    }

    /**
     * 获取专项检查问题详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:checkQuestion:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ptCheckQuestionService.selectPtCheckQuestionById(id));
    }

    /**
     * 新增专项检查问题
     */
    @PreAuthorize("@ss.hasPermi('patrol:checkQuestion:add')")
    @Log(title = "专项检查问题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtCheckQuestion ptCheckQuestion) {
        return toAjax(ptCheckQuestionService.insertPtCheckQuestion(ptCheckQuestion));
    }

    /**
     * 修改专项检查问题
     */
    @PreAuthorize("@ss.hasPermi('patrol:checkQuestion:edit')")
    @Log(title = "专项检查问题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtCheckQuestion ptCheckQuestion) {
        return toAjax(ptCheckQuestionService.updatePtCheckQuestion(ptCheckQuestion));
    }

    /**
     * 删除专项检查问题
     */
    @PreAuthorize("@ss.hasPermi('patrol:checkQuestion:remove')")
    @Log(title = "专项检查问题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ptCheckQuestionService.deletePtCheckQuestionByIds(ids));
    }
}
